﻿#include "stdafx.h"
#include "OgreFrame.h"
#include "GraphicScriptBinder.h"

_NAMESPACE_BEGIN

OGREApplication::OGREApplication()
{
	mFrameListener = 0;
	mRoot = 0;
	mResourcePath = "";
}

OGREApplication::~OGREApplication()
{
	if (mFrameListener)
		delete mFrameListener;
	if (mRoot)
		OGRE_DELETE mRoot;
}

void OGREApplication::go( void )
{
	if (!setup())
		return;

	mRoot->startRendering();

	destroyScene();
}

bool OGREApplication::setup( void )
{
	String pluginsPath;
	// only use plugins.cfg if not static
#ifndef OGRE_STATIC_LIB
#ifdef _DEBUG
	pluginsPath = mResourcePath + "plugins_debug.cfg";
#else 
	pluginsPath = mResourcePath + "plugins.cfg";
#endif
#endif

	mRoot = OGRE_NEW Root(pluginsPath, 
		mResourcePath + "ogre.cfg", mResourcePath + "Ogre.log");

	setupResources();

	bool carryOn = configure();
	if (!carryOn) return false;

	chooseSceneManager();
	createCamera();

	// Set default mipmap level (NB some APIs ignore this)
	TextureManager::getSingleton().setDefaultNumMipmaps(5);

	// Create any resource listeners (for loading screens)
	createResourceListener();

	// Load resources
	SAVE_LOG("载入资源");
	loadResources();
	SAVE_LOG("载入资源完成");

	// Create the scene
	createScene();

	createFrameListener();

	return true;

}

bool OGREApplication::configure( void )
{
	// 显示配置对话框并初始化系统
	// 你可以跳过这一步而使用root.restoreConfig()来载入配置
	// 当然你得确定orge.cfg里有可用的配置
	if(mRoot->showConfigDialog())
	{
		// 用户单击OK则返回true,并开始初始化
		// 在这里我们创建一个默认的渲染窗口
		mWindow = mRoot->initialise(true);
		assert(mWindow);
		OgreProperty::Instance()->window = mWindow;

		return true;
	}
	else
	{
		return false;
	}
}

void OGREApplication::chooseSceneManager( void )
{
	// 创建一个一般的场景管理器
	mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "ExampleSMInstance");
}

void OGREApplication::createCamera( void )
{
	// 创建摄像机
	mCamera = mSceneMgr->createCamera("PlayerCam");

	// 摄像机位置
	mCamera->setPosition(Vector3(0,0,500));
	// 观察点
	mCamera->lookAt(Vector3(0,0,-300));
	mCamera->setNearClipDistance(5);
}

void OGREApplication::createViewports( void )
{
	// 创建视口
	Viewport* vp = mWindow->addViewport(mCamera);
	vp->setBackgroundColour(ColourValue(0,0,0));

	// 更改摄像机的纵横高度比来匹配视口
	mCamera->setAspectRatio(
		Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}

void OGREApplication::setupResources( void )
{
	// Load resource paths from config file
	ConfigFile cf;
	cf.load(mResourcePath + "resources.cfg");

	// Go through all sections & settings in the file
	ConfigFile::SectionIterator seci = cf.getSectionIterator();

	String secName, typeName, archName;
	while (seci.hasMoreElements())
	{
		secName = seci.peekNextKey();
		ConfigFile::SettingsMultiMap *settings = seci.getNext();
		ConfigFile::SettingsMultiMap::iterator i;
		for (i = settings->begin(); i != settings->end(); ++i)
		{
			typeName = i->first;
			archName = i->second;

			ResourceGroupManager::getSingleton().addResourceLocation(
				archName, typeName, secName);

		}
	}
}

void OGREApplication::loadResources( void )
{
	// 初始化，分析脚本等
	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void OGREApplication::createResourceListener( void )
{

}

void OGREApplication::createFrameListener( void )
{
	mFrameListener= new OGREFrameListener(mWindow, mCamera);
	mRoot->addFrameListener(mFrameListener);
}

void OGREApplication::destroyScene( void )
{
	SAVE_LOG("释放场景成功");
}
_NAMESPACE_END